'use strict'

const path = require('path')

const fs = require('fs')

const captchapng = require('captchapng')

// exports.getLoginPage = (req, res) => {

//     fs.readFile(path.join(__dirname, '../views/login.html'), (err, data) => {
//         res.setHeader('Content-Type', 'text/html;charset=utf8')
//         res.end(data)
//     })
// }
//获取登录页面exports

exports.getLoginPage = (req, res) => {
    //读取我们的login.html,并且返回给浏览器呈现
    fs.readFile(path.join(__dirname, '../views/login.html'), (err, data) => {
        res.setHeader('Content-Type', 'text/html;charset=utf8')
        res.end(data)
    })
}


exports.getVcodeImage = (req, res) => {
    const vcode = parseInt(Math.random() * 9000 + 1000)
    console.log(vcode)
    //利用我们刚刚开辟 的内存空间，存储我们的验证码数字
    req.session.vcode = vcode
   
    
    var p = new captchapng(80, 30, vcode); // width,height,numeric captcha 
    p.color(0, 0, 0, 0); // First color: background (red, green, blue, alpha) 
    p.color(80, 80, 80, 255); // Second color: paint (red, green, blue, alpha) 

    var img = p.getBase64();
    var imgbase64 = new Buffer(img, 'base64');
    //res.setHeader()
    res.writeHead(200, {
        'Content-Type': 'image/png'
    });
    res.end(imgbase64);
}


exports.login=(req,res)=>{
    const vcodeClient=parseInt(req.body.vcode||'')
    const vcodServer=req.session.vcode
    console.log(123) 
    if(vcodeClient==vcodServer){
        res.json({
            status:1,
            message:'验证成功'
        })
    }
    else{
        res.json({
             status:0,
            message:'验证失败'
        })
    }
}